package cn.edu.ctbu.secondclass.webapi;
import cn.edu.ctbu.secondclass.domain.Student;
import cn.edu.ctbu.secondclass.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@RestController
@RequestMapping("/webapi/student")
public class StudentRestController {
    @Autowired
    private StudentService studentService;
/*
读取的url：、webapi student list
 */
    @GetMapping("/list")
    public List<Student> getAll(){
        List<Student> students=studentService.findAll();
        return students;

    }
    @GetMapping("/get/{id}")
    public Student getAll(@PathVariable Integer id){
        Student student=studentService.getById(id);
        student.setPassword("");

        return student;

    }





    @PostMapping("/insert")
    public Student insert(Student student){

        Student student1=studentService.insert(student);

        return student1;

    }

    @PutMapping("/update")
    public Student update(Student student){
        //读取旧的数据
        Student oldstudent=studentService.getById(student.getId());

        if (StringUtils.isEmpty(student.getPassword())){
            student.setPassword(oldstudent.getPassword());
        }
        student=studentService.update(student);
        return student;

    }

    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Integer id){

        studentService.delete(id);



    }
}

